﻿/**
 * Created by xjchen on 2020-03-03.
 * */

#ifndef MIDDLEWARE_HEADER_H
#define MIDDLEWARE_HEADER_H

#define INLINE inline
#define FORCEINLINE inline __attribute__ ((always_inline))

#define GCC_PACK(n) __attribute__((packed,aligned(n)))
#define GCC_ALIGN(n) __attribute__((aligned(n)))

#define SAFE_DELETE(p) do { if(p){ delete (p); (p) = nullptr;}} while(0)

/** 流图连接及设置的API，用于AppDelegate::onStart()，以及flow.design文件中 */
#define FLOW_API

/** 用户在模块计算时用于查询及控制等的API */
#define USER_API

#define STRINGIFY(var)  #var
#define CONCATENATE(arg1, arg2)  arg1##arg2
#define CONCATENATE1(arg1, arg2)  CONCATENATE(arg1, arg2)
#define CONCATENATE2(arg1, arg2)  CONCATENATE1(arg1, arg2)
#define MIN(a,b)  ((a)<(b)?(a):(b))
#define MAX(a,b)  ((a)>(b)?(a):(b))

#define FOR_EACH_1(fun,a) \
        fun(a);
#define FOR_EACH_2(fun,a,b) \
        fun(a); FOR_EACH_1(fun,b)
#define FOR_EACH_3(fun,a,b,c) \
        fun(a); FOR_EACH_2(fun,b,c)
#define FOR_EACH_4(fun,a,b,c,d) \
        fun(a); FOR_EACH_3(fun,b,c,d)
#define FOR_EACH_5(fun,a,b,c,d,e) \
        fun(a); FOR_EACH_4(fun,b,c,d,e)
#define FOR_EACH_6(fun,a,b,c,d,e,f) \
        fun(a); FOR_EACH_5(fun,b,c,d,e,f)
#define FOR_EACH_7(fun,a,b,c,d,e,f,g) \
        fun(a); FOR_EACH_6(fun,b,c,d,e,f,g)
#define FOR_EACH_8(fun,a,b,c,d,e,f,g,h) \
        fun(a); FOR_EACH_7(fun,b,c,d,e,f,g,h)
#define FOR_EACH_9(fun,a,b,c,d,e,f,g,h,i) \
        fun(a); FOR_EACH_8(fun,b,c,d,e,f,g,h,i)
#define FOR_EACH_10(fun,a,b,c,d,e,f,g,h,i,j) \
        fun(a); FOR_EACH_9(fun,b,c,d,e,f,g,h,i,j)
#define FOR_EACH_11(fun,a,b,c,d,e,f,g,h,i,j,k) \
        fun(a); FOR_EACH_10(fun,b,c,d,e,f,g,h,i,j,k)
#define FOR_EACH_12(fun,a,b,c,d,e,f,g,h,i,j,k,l) \
        fun(a); FOR_EACH_11(fun,b,c,d,e,f,g,h,i,j,k,l)
#define FOR_EACH_13(fun,a,b,c,d,e,f,g,h,i,j,k,l,m) \
        fun(a); FOR_EACH_12(fun,b,c,d,e,f,g,h,i,j,k,l,m)
#define FOR_EACH_14(fun,a,b,c,d,e,f,g,h,i,j,k,l,m,n) \
        fun(a); FOR_EACH_13(fun,b,c,d,e,f,g,h,i,j,k,l,m,n)
#define FOR_EACH_15(fun,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o) \
        fun(a); FOR_EACH_14(fun,b,c,d,e,f,g,h,i,j,k,l,m,n,o)
#define FOR_EACH_16(fun,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p) \
        fun(a); FOR_EACH_15(fun,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p)
#define FOR_EACH_17(fun,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q) \
        fun(a); FOR_EACH_16(fun,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q)
#define FOR_EACH_18(fun,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r) \
        fun(a); FOR_EACH_17(fun,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r)
#define FOR_EACH_19(fun,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s) \
        fun(a); FOR_EACH_18(fun,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s)
#define FOR_EACH_20(fun,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t) \
        fun(a); FOR_EACH_19(fun,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t)
#define FOR_EACH_21(fun,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u) \
        fun(a); FOR_EACH_20(fun,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u)
#define FOR_EACH_22(fun,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v) \
        fun(a); FOR_EACH_21(fun,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v)
#define FOR_EACH_23(fun,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w) \
        fun(a); FOR_EACH_22(fun,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w)
#define FOR_EACH_24(fun,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x) \
        fun(a); FOR_EACH_23(fun,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x)
#define FOR_EACH_25(fun,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y) \
        fun(a); FOR_EACH_24(fun,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y)
#define FOR_EACH_26(fun,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z) \
        fun(a); FOR_EACH_25(fun,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z)
#define FOR_EACH_27(fun,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,aa) \
        fun(a); FOR_EACH_26(fun,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,aa)
#define FOR_EACH_28(fun,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,aa,ab) \
        fun(a); FOR_EACH_27(fun,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,aa,ab)
#define FOR_EACH_29(fun,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,aa,ab,ac) \
        fun(a); FOR_EACH_28(fun,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,aa,ab,ac)
#define FOR_EACH_30(fun,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,aa,ab,ac,ad) \
        fun(a); FOR_EACH_29(fun,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,aa,ab,ac,ad)
#define FOR_EACH_31(fun,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,aa,ab,ac,ad,ae) \
        fun(a); FOR_EACH_30(fun,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,aa,ab,ac,ad,ae)
#define FOR_EACH_32(fun,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,aa,ab,ac,ad,ae,af) \
        fun(a); FOR_EACH_31(fun,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,aa,ab,ac,ad,ae,af)

#define EX_FOR_EACH_1(ex,fun,a) \
        fun(ex,a);
#define EX_FOR_EACH_2(ex,fun,a,b) \
        fun(ex,a); EX_FOR_EACH_1(ex,fun,b)
#define EX_FOR_EACH_3(ex,fun,a,b,c) \
        fun(ex,a); EX_FOR_EACH_2(ex,fun,b,c)
#define EX_FOR_EACH_4(ex,fun,a,b,c,d) \
        fun(ex,a); EX_FOR_EACH_3(ex,fun,b,c,d)
#define EX_FOR_EACH_5(ex,fun,a,b,c,d,e) \
        fun(ex,a); EX_FOR_EACH_4(ex,fun,b,c,d,e)
#define EX_FOR_EACH_6(ex,fun,a,b,c,d,e,f) \
        fun(ex,a); EX_FOR_EACH_5(ex,fun,b,c,d,e,f)
#define EX_FOR_EACH_7(ex,fun,a,b,c,d,e,f,g) \
        fun(ex,a); EX_FOR_EACH_6(ex,fun,b,c,d,e,f,g)
#define EX_FOR_EACH_8(ex,fun,a,b,c,d,e,f,g,h) \
        fun(ex,a); EX_FOR_EACH_7(ex,fun,b,c,d,e,f,g,h)
#define EX_FOR_EACH_9(ex,fun,a,b,c,d,e,f,g,h,i) \
        fun(ex,a); EX_FOR_EACH_8(ex,fun,b,c,d,e,f,g,h,i)
#define EX_FOR_EACH_10(ex,fun,a,b,c,d,e,f,g,h,i,j) \
        fun(ex,a); EX_FOR_EACH_9(ex,fun,b,c,d,e,f,g,h,i,j)
#define EX_FOR_EACH_11(ex,fun,a,b,c,d,e,f,g,h,i,j,k) \
        fun(ex,a); EX_FOR_EACH_10(ex,fun,b,c,d,e,f,g,h,i,j,k)
#define EX_FOR_EACH_12(ex,fun,a,b,c,d,e,f,g,h,i,j,k,l) \
        fun(ex,a); EX_FOR_EACH_11(ex,fun,b,c,d,e,f,g,h,i,j,k,l)
#define EX_FOR_EACH_13(ex,fun,a,b,c,d,e,f,g,h,i,j,k,l,m) \
        fun(ex,a); EX_FOR_EACH_12(ex,fun,b,c,d,e,f,g,h,i,j,k,l,m)
#define EX_FOR_EACH_14(ex,fun,a,b,c,d,e,f,g,h,i,j,k,l,m,n) \
        fun(ex,a); EX_FOR_EACH_13(ex,fun,b,c,d,e,f,g,h,i,j,k,l,m,n)
#define EX_FOR_EACH_15(ex,fun,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o) \
        fun(ex,a); EX_FOR_EACH_14(ex,fun,b,c,d,e,f,g,h,i,j,k,l,m,n,o)
#define EX_FOR_EACH_16(ex,fun,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p) \
        fun(ex,a); EX_FOR_EACH_15(ex,fun,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p)
#define EX_FOR_EACH_17(ex,fun,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q) \
        fun(ex,a); EX_FOR_EACH_16(ex,fun,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q)
#define EX_FOR_EACH_18(ex,fun,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r) \
        fun(ex,a); EX_FOR_EACH_17(ex,fun,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r)
#define EX_FOR_EACH_19(ex,fun,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s) \
        fun(ex,a); EX_FOR_EACH_18(ex,fun,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s)
#define EX_FOR_EACH_20(ex,fun,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t) \
        fun(ex,a); EX_FOR_EACH_19(ex,fun,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t)
#define EX_FOR_EACH_21(ex,fun,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u) \
        fun(ex,a); EX_FOR_EACH_20(ex,fun,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u)
#define EX_FOR_EACH_22(ex,fun,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v) \
        fun(ex,a); EX_FOR_EACH_21(ex,fun,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v)
#define EX_FOR_EACH_23(ex,fun,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w) \
        fun(ex,a); EX_FOR_EACH_22(ex,fun,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w)
#define EX_FOR_EACH_24(ex,fun,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x) \
        fun(ex,a); EX_FOR_EACH_23(ex,fun,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x)
#define EX_FOR_EACH_25(ex,fun,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y) \
        fun(ex,a); EX_FOR_EACH_24(ex,fun,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y)
#define EX_FOR_EACH_26(ex,fun,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z) \
        fun(ex,a); EX_FOR_EACH_25(ex,fun,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z)
#define EX_FOR_EACH_27(ex,fun,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,aa) \
        fun(ex,a); EX_FOR_EACH_26(ex,fun,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,aa)
#define EX_FOR_EACH_28(ex,fun,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,aa,ab) \
        fun(ex,a); EX_FOR_EACH_27(ex,fun,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,aa,ab)
#define EX_FOR_EACH_29(ex,fun,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,aa,ab,ac) \
        fun(ex,a); EX_FOR_EACH_28(ex,fun,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,aa,ab,ac)
#define EX_FOR_EACH_30(ex,fun,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,aa,ab,ac,ad) \
        fun(ex,a); EX_FOR_EACH_29(ex,fun,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,aa,ab,ac,ad)
#define EX_FOR_EACH_31(ex,fun,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,aa,ab,ac,ad,ae) \
        fun(ex,a); EX_FOR_EACH_30(ex,fun,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,aa,ab,ac,ad,ae)
#define EX_FOR_EACH_32(ex,fun,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,aa,ab,ac,ad,ae,af) \
        fun(ex,a); EX_FOR_EACH_31(ex,fun,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,aa,ab,ac,ad,ae,af)

#define FOR_EACH_NARG(...) FOR_EACH_NARG_EXPAND((__VA_ARGS__,\
        32,31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0))
#define FOR_EACH_NARG_EXPAND(args) FOR_EACH_ARG_N args
#define FOR_EACH_ARG_N(_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,\
        _17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,N, ...) N

#define FOR_EACH_EXPAND(x,y)    x y
#define FOR_EACH(fun, ...) FOR_EACH_EXPAND(CONCATENATE2(FOR_EACH_,FOR_EACH_NARG(__VA_ARGS__)),(fun, __VA_ARGS__))
#define EX_FOR_EACH(ex, fun, ...) FOR_EACH_EXPAND(CONCATENATE2(EX_FOR_EACH_,FOR_EACH_NARG(__VA_ARGS__)),(ex, fun, __VA_ARGS__))


#endif /** MIDDLEWARE_HEADER_H */
